package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.Visit;
import com.yixin.hospital.entity.dto.req.visit.VisitAddReqDTO;
import com.yixin.hospital.entity.dto.req.visit.VisitHerbalDrugReqDTO;
import com.yixin.hospital.entity.dto.req.visit.VisitPatientCheckReqDTO;
import com.yixin.hospital.entity.dto.req.visit.VisitSubPackageDrugReqDTO;
import com.yixin.hospital.service.VisitService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/visit")
@Api(value = "就诊管理", tags = "就诊管理")
public class VisitController {

    @Resource
    private VisitService visitService;


    @PostMapping("/add-visit")
    @ApiOperation(value = "新增就诊")
    @Transactional
    public ApiResult<?> add(@Valid @RequestBody VisitAddReqDTO visitAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitService.add(visitAddReqDTO, userName);
    }

    @PostMapping("/update-visit-status")
    @ApiOperation(value = "更新就诊状态")
    @Transactional
    public ApiResult<?> updateStatus(@Valid @RequestBody VisitAddReqDTO visitAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitService.updateStauts(visitAddReqDTO, userName);
    }

    @GetMapping("/detail-visit")
    @ApiOperation(value = "就诊信息详情")
    public ApiResult<?> detail(@RequestParam("guidanceCode") String guidanceCode) {
        Visit visit = visitService.getOne(new LambdaQueryWrapper<Visit>().eq(Visit::getGuidanceCode, guidanceCode));
        return ApiResult.success(visit);
    }

    @GetMapping("/detail-visit-drug")
    @ApiOperation(value = "查询整装分装记录")
    public ApiResult<?> detailVisitDrug(@RequestParam("guidanceCode") String guidanceCode) {
        return visitService.detailVisitDrug(guidanceCode);
    }

    @PostMapping("/add-visit-drug")
    @ApiOperation(value = "新增就诊药品（分装 整装）")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<?> addVisitSubPackageDrug(@Valid @RequestBody VisitSubPackageDrugReqDTO visitSubPackageDrugReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitService.addVisitSubPackageDrug(visitSubPackageDrugReqDTO, userId, userName);
    }

    @PostMapping("/add-visit-herbal-drug")
    @ApiOperation(value = "新增就诊就诊中草药")
    @Transactional
    public ApiResult<?> addVisitHerbalDrug(@Valid @RequestBody VisitHerbalDrugReqDTO visitHerbalDrugReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitService.addVisitHerbalDrug(visitHerbalDrugReqDTO, userName);
    }

    @PostMapping("/add-visit-patient-check")
    @ApiOperation(value = "新增就诊患者诊断")
    @Transactional
    public ApiResult<?> addVisitPatientCheck(@Valid @RequestBody VisitPatientCheckReqDTO visitPatientCheckReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitService.addVisitPatientCheck(visitPatientCheckReqDTO, userName);
    }

}
